<?php
class GoodsControl extends AuthControl{
	//商品列表
	public function index(){
		$Model = K("Goods");
		$page = new Page($Model->count(),10);
		$data = $Model->limit($page->limit())->all();
		$this->assign("page",$page->show(2));
		$this->assign('data',$data);
		$this->display(); 
	}

	//添加商品
	public function add(){
		if(IS_POST){
			// p($_POST);exit;  
			$Model=K('Goods');
			if($Model->addGoods()){
				$this->success('添加成功'); 
			}else{
				$this->error($Model->error);
			}
		}else{
			$cid=Q('cid',0,'intval'); 
			//获取缓存中栏目数据
			$category=cache('category');
			// p($category);exit;
			$Model=K('GoodsAttrForm');
			//商品品牌数据
			$brand=M('brand')->all();
			//获得栏目cid关联的属性值
			$gaf=$Model->get($category[$cid]['gtid']); 
			// 获得规格表单处理
			$GoodsSpec=K('GoodsSpec');

			$goodsSpec=$GoodsSpec->get($category[$cid]['gtid']);
			$this->assign('goodsSpec',$goodsSpec);
			$this->assign('GoodsAttrForm',$gaf);
			//选择栏目
			$this->assign('category',$category[$cid]);
			$this->assign('brand',$brand); 
			// exit;
			$this->display();
		} 
	}
	//修改商品
	public function edit(){
		 if(IS_POST){
		 	// p($_POST);exit;
		 	$Model=K('Goods');
			if($Model->editGoods()){
				$this->success('修改成功');
			}else{
				$this->error($Model->error); 
			}
		 }else{ 
		 	$gid=Q('gid',0,'intval');
		 	$Model=K('Goods');
		 	$db=M('goods_pic')->where("gid=".$gid)->all();
		 	$data=$Model->find($gid);
		 	$data['addtime']=date("Y/m/d h:i:s");
		 	$category=cache('category');
		 	$gtid=$category[$data['cid']]['gtid'];
		 	$goodsAttr=M('goods_attr')->where(array('gid'=>$gid))->getField('avid,value');
		 	//没规格的
		 	$stockModel=K('GoodsAttrForm');
		 	$stockData=$stockModel->get($gtid,$goodsAttr);
		 	// 有规格的
		 	$specStockModel=K('stockSpec');
		 	$specStockData=$specStockModel->get($gtid,$gid); 
		 	//库存属性值
		 	$stock=M('stock')->where(array('gid'=>$gid))->all();
		 	// p($goodsAttr);
		 	// p($specStockData);exit;
		 	$this->assign('price',$stock); 
		 	$this->assign('stock',$stockData); 
		 	$this->assign('specStock',$specStockData);  
		 	$this->assign("pic",$db);
		 	$this->assign("field",$data);
		 	$this->assign("category",$category);
		 	$this->display();
		 }
	}
	//查找栏目
	public function selectCategory(){
		$category=cache('category');
		$this->assign('category',$category);
		$this->display();
	}
	//添加品牌
	public function addBrand(){
		
		if(IS_POST){
			// p($_FILES);exit;
			$Model=M('brand');
			$Model->validate=array(
				array('bname','nonull','品牌名不能为空',2,3)
				);
			if($Model->validate()){
				$d=$Model->add();
				if(!empty($_FILES['logo']['tmp_name'])){
				K('Goods')->logo($d); 
				}
				$this->success("添加成功");
			}else{
				$this->error($Model->error);
			}
		
		}else{
			$this->display();
		}
	}
	// 删除商品
	public function delete(){
		$gid=Q('gid',0,'intval');
		// p($gid);exit;
		if($gid){
			$Model=K('Goods');
			if($Model->drop()){
				$this->error('删除成功');
			}
		}else{ 
			$this->error($Model->error); 
		}
	}
	//处理图片
	public function pic(){
		$pid=Q('pid',0,'intval');
		$Model=M('goods_pic');
		$db=$Model->find($pid);
		if(is_file($db['big'])){
			unlink($db['big']);
		}
		if(is_file($db['medium'])){
			unlink($db['medium']);
		}
		if(is_file($db['small'])){
			unlink($db['small']);
		}
		$Model->del($pid);
		exit;
	}












}